#include "stdafx.h"
#include "svgzbrowser.h"

SVGZBrowser::SVGZBrowser(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    this->setAcceptDrops(true);

    auto args = QApplication::instance()->arguments();

    if (args.size() > 1)
    {
        auto filepath = args[1];
        if (QFile::exists(filepath))
        {
            showImage(filepath);
        }
    }
}

SVGZBrowser::~SVGZBrowser()
{

}

void SVGZBrowser::dragEnterEvent(QDragEnterEvent * event)
{
    if (event->mimeData()->hasFormat("text/uri-list"))
        event->acceptProposedAction();
}

void SVGZBrowser::dropEvent(QDropEvent * event)
{
    QList<QUrl> urls = event->mimeData()->urls();
    if (urls.isEmpty())
        return;
    QString fileName = urls.first().toLocalFile();
    if (fileName.isEmpty())
        return;

    showImage(fileName);
}

void SVGZBrowser::showImage(QString fileName)
{
    QPixmap p(fileName);
    this->ui.sizeText->setText(QString("%1 X %2").arg(p.width()).arg(p.height()));


    if (p.width() > this->ui.imageView->width())
    {
        p = p.scaledToWidth(this->ui.imageView->width());
        this->ui.imageView->setPixmap(p);
        this->ui.imageView->setFixedSize(p.width(), p.height());
        this->adjustSize();
    }
    else
        this->ui.imageView->setPixmap(p);
}
